Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

devalue

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

devalue

Gets the job done when JSON.stringify can't

  • 4.3.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3M
decreased by-13.9%
Maintainers
1
Weekly downloads
 
Created

What is devalue?

The devalue npm package is used for serializing complex JavaScript data structures into a string form that can be evaluated to produce an equivalent structure. This is particularly useful for cases where JSON.stringify() is not sufficient, such as when dealing with circular references, Dates, RegExps, Maps, Sets, or functions. It's designed to handle cases that JSON struggles with, making it a powerful tool for certain serialization needs.

What are devalue's main functionalities?

Serializing and deserializing complex objects

This feature demonstrates how devalue can serialize a complex object into a string and then deserialize it back into an object. Unlike JSON.stringify, devalue can handle circular references and other complex types not supported by JSON.

"const devalue = require('devalue');\nconst obj = { foo: 'bar', baz: { qux: 'quux' } };\nconst serialized = devalue(obj);\nconsole.log(serialized); // Output: '{foo:\"bar\",baz:{qux:\"quux\"}}'\neval('var restored = ' + serialized);\nconsole.log(restored); // Output: { foo: 'bar', baz: { qux: 'quux' } }"

Handling circular references

This example shows devalue's ability to handle circular references within objects, which JSON.stringify cannot do without throwing an error.

"const devalue = require('devalue');\nconst obj = {};\nobj.self = obj;\nconst serialized = devalue(obj);\nconsole.log(serialized); // Output: 'var x={};x.self=x;x'\neval('var restored = ' + serialized);\nconsole.log(restored.self === restored); // Output: true"

Serializing special types (Date, RegExp, Map, Set)

This feature highlights devalue's capability to serialize JavaScript's special object types like Date, RegExp, Map, and Set, which are not natively supported by JSON.stringify.

"const devalue = require('devalue');\nconst obj = {\n  date: new Date(),\n  regex: /test/i,\n  map: new Map([['key', 'value']]),\n  set: new Set([1, 2, 3])\n};\nconst serialized = devalue(obj);\nconsole.log(serialized); // Output includes serialized forms of Date, RegExp, Map, and Set"

Other packages similar to devalue

FAQs

Package last updated on 19 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc